package com.corpgovernment.organization.dataloader;
import com.corpgovernment.api.basic.soa.OssServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Component
@Slf4j
public class BasicOssClientLoader {

	@Autowired
	private OssServiceClient ossServiceClient;

	/**
	 * 调用基础服务--上传文件
	 * @return
	 */
	public String uploadFile(MultipartFile file) {
		String result = ossServiceClient.uploadFile(file);
		if (result == null ) {
			log.error("调用基础服务[上传文件]接口异常");
			return null;
		}
		return result;
	}

	/**
	 * 上传到私有资源文件夹，返回 相对路径
	 * @return
	 */
	public String putPrivateObject(MultipartFile file) {
		String result = ossServiceClient.uploadSafeFile(file);
		if (result == null ) {
			log.error("调用基础服务[上传文件]接口异常");
			return null;
		}
		return result;
	}


	/**
	 * 获取有效期的私有资源的url
	 * @return
	 */
	public String privateGeneratePresignedUrl(String file) {
		String result = null;
		try {
			result = ossServiceClient.getUploadSafeUrl(file);
		} catch (Exception e) {
			log.error("调用基础服务[获取链接url]接口异常");
			return null;
		}
		if (result == null ) {
			log.error("调用基础服务[获取链接url]接口异常");
			return null;
		}
		return result;
	}

}